<template>
  <view>
    <my-search @click="goToSearch"></my-search>
    <!-- <my-search @click="goToSearch" bgColor="pink" radius="5"></my-search> -->
    <view class="scroll-view-container">
      <!-- 左侧scroll -->
      <scroll-view scroll-y="true" :style="{height: wh + 'px'}" class="left-scroll-view">
        <!-- 用block包裹起来， 他不会渲染到页面上，只接受控制属性 -->
        <block v-for="(cate, i) in cateList" :key="cate.cat_id">
          <!-- 当点击的时候将i赋值给active， 通过判断他们是否相等来动态添加激活的类名 -->
          <view :class="['left-scroll-view-item', i === active ? 'active' : '']" @click="changeActive(i)">
            {{cate.cat_name}}
          </view>
        </block>
      </scroll-view>
      <!-- 右侧scroll -->
      <scroll-view scroll-y="true" :style="{height: wh + 'px'}" class="right-scroll-view" :scroll-top="scrTop">
        <!-- 二级分类 -->
        <view v-for="(cate2, i2) in cateLev2" :key="cate2.cat_id" class="cate-lev2-container">
          <!-- 二级分类标题 -->
          <view class="cate-lev2-title">/ {{cate2.cat_name}} /</view>
          <!-- 三级分类详情 -->
          <view class="cate-lev3-list">
            <view class="cate-lev3-container" v-for="(cate3, i) in cate2.children" :key="cate3.cat_id"
              @click="goToGoodsList(cate3)">
              <image :src="cate3.cat_icon"></image>
              <text>{{cate3.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import tarbarBarge from '../../mixins/tarbar-barge.js'
  export default {
    mixins: [tarbarBarge],
    data() {
      return {
        wh: 0, // 设置可用窗口的高度
        cateList: [],
        active: 0,
        cateLev2: [],
        scrTop: 0
      };
    },
    onLoad() {
      // 获取当前系统的信息
      const systemInfo = uni.getSystemInfoSync()
      this.wh = systemInfo.windowHeight - 50
      // 获取左侧分类的列表
      this.getCateList()
    },
    methods: {
      async getCateList() {
        const {
          data
        } = await uni.$http.get('/api/public/v1/categories')
        if (data.meta.status !== 200) return uni.$showMessage()
        // 成功后赋值
        this.cateList = data.message
        // 二级分类展示
        this.cateLev2 = data.message[0].children
      },
      changeActive(i) {
        this.active = i
        // 二级分类展示
        this.cateLev2 = this.cateList[i].children
        // 每次点击一级分类的时候重置scroll-top
        // 如果单纯设置为0的话， 之前也是0，所以不会生效， 我们取巧的设置0和1变换，来达到效果
        this.scrTop = this.scrTop === 0 ? 1 : 0
      },
      // 点击三级分类 跳转到商品列表页
      goToGoodsList(cate) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?cat_id=' + cate.cat_id
        })
      },
      // 点击搜索去往搜索分包页面
      goToSearch() {
        // console.log('ok')
        // 去往出tarBar之外的页面，都是用navigatorTo方法
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
  .scroll-view-container {
    display: flex;

    .left-scroll-view {
      width: 120px;

      .left-scroll-view-item {
        height: 60px;
        background-color: #f7f7f7;
        line-height: 60px;
        text-align: center;
        font-size: 12px;

        &.active {
          position: relative;
          background-color: #FFFFFF;

          &::before {
            position: absolute;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
            content: ' ';
            display: block;
            width: 3px;
            height: 30px;
            background-color: #c00000;
          }
        }
      }
    }

    // 右侧
    .right-scroll-view {
      .cate-lev2-container {
        .cate-lev2-title {
          text-align: center;
          margin: 10px 0;
        }

        .cate-lev3-list {
          display: flex;
          flex-wrap: wrap;

          .cate-lev3-container {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            width: 33.33%;
            margin-bottom: 10px;

            image {
              width: 60px;
              height: 60px;
            }

            text {
              font-size: 12px;
            }
          }
        }
      }
    }
  }
</style>
